var weekOfYear = function(dt) {
	var calDay = dt || new Date(); // 当前要计算的时间
	var firstDay = new Date(calDay.getFullYear(), 0, 1); // 本年第一天
	var daysAll = Math.floor((calDay - firstDay) / 1000 / 60 / 60 / 24) + 1;
	var firstDayWeekday = firstDay.getDay();
	var diffDay = firstDayWeekday == 0 ? 6 : firstDayWeekday - 1;
	daysAll = daysAll + diffDay;
	return Math.ceil(daysAll / 7); // 返回计算结果
}

function dateFromWeek(year, week, day) {
	var date1 = new Date(year, 0, 1);
	var dayMS = 24 * 60 * 60 * 1000;
	var firstDay = (7 - date1.getDay()) * dayMS;
	var weekMS = (week - 2) * 7 * dayMS;
	var result = date1.getTime() + firstDay + weekMS + day * dayMS;
	date1.setTime(result);
	return date1;
}

function nextMondayFromDate(today) {
	var week = weekOfYear();
	var monday = dateFromWeek(today.getFullYear(), week, 1);

	var next_monday = new Date();
	next_monday.setDate(monday.getDate() + 7);
	var month = next_monday.getMonth() + 1;
	var date = next_monday.getDate();
	if (month < 10) {
		month = "0" + month;
	}
	if (date < 10) {
		date = "0" + date;
	}
	var format_monday = next_monday.getFullYear() + "-" + month + "-" + date;

	return format_monday;
}

function nexFridayFromDate(today) {
	var week = weekOfYear();
	var friday = dateFromWeek(today.getFullYear(), week, 5);

	var next_friday = new Date();
	next_friday.setDate(friday.getDate() + 7);
	var month = next_friday.getMonth() + 1;
	var date = next_friday.getDate();
	if (month < 10) {
		month = "0" + month;
	}
	if (date < 10) {
		date = "0" + date;
	}
	var format_friday = next_friday.getFullYear() + "-" + month + "-" + date;

	return format_friday;
}

function currMondayFromDate(today) {
	var week = weekOfYear();
	var monday = dateFromWeek(today.getFullYear(), week, 1);

	var curr_monday = new Date();
	curr_monday.setDate(monday.getDate());
	var month = curr_monday.getMonth() + 1;
	var date = curr_monday.getDate();
	if (month < 10) {
		month = "0" + month;
	}
	if (date < 10) {
		date = "0" + date;
	}
	var format_monday = "" + curr_monday.getFullYear() + month + date;

	return format_monday;
}

function currFridayFromDate(today) {
	var week = weekOfYear();
	var friday = dateFromWeek(today.getFullYear(), week, 5);

	var curr_friday = new Date();
	curr_friday.setDate(friday.getDate());
	var month = curr_friday.getMonth() + 1;
	var date = curr_friday.getDate();
	if (month < 10) {
		month = "0" + month;
	}
	if (date < 10) {
		date = "0" + date;
	}
	var format_friday = "" + curr_friday.getFullYear() + month + date;

	return format_friday;
}
function yearMonthFormat(value) {
	// 把"200903"格式的日期转换成"2009年03月"
	return Date.parseDate(value, 'Ym').format('Y年m月');

}
function yearAndMonth(value) {
	var date = Date.parseDate(value, 'Ym');
	var year = (date.getYear() < 1900) ? (date.getYear() + 1900) : date
			.getYear();
	var month = date.getMonth() + 1;
	var yearMonth = [ year, month ];
	return yearMonth;
}

function check() {
	var event = (event) ? event : window.event;
	var currentKey = (event.charCode) ? event.charCode : event.keyCode;
	if (currentKey == 13 || currentKey == 46)
		return true;
	if (currentKey < 48 || window.event.keyCode > 57)
		return false;
	else
		return true;
}

// 传入的日期格式 yyyy-MM-dd,返回一个星期一的日期对象
function getMonday(date) {
	var strDate = date.split("-");
	var d = new Date(strDate[0], (strDate[1] - parseInt(1)), strDate[2]);
	var num = d.getDay();
	if (num == 0) {
		d.setDate(d.getDate() - 6);
	} else {
		var dt = d.getDate() - num + 1;
		d.setDate(dt);
	}
	return d;
}

/*得到这个月份的第一天 传入的日期格式 yyyy-MM-dd */
function getMonthFirst(date){
        var strDate = date.split("-");
        var d = new Date(strDate[0], (strDate[1] - parseInt(1)), strDate[2]);
        var obj = new Date(d.getTime());
        returns = new Date(obj.setDate(1));
        return returns;
}
/*得到这个月份的最后一天*/
function getMonthLast(date){
         var strDate = date.split("-");
        var d = new Date(strDate[0], (strDate[1] - parseInt(1)), strDate[2]);
        var returns = null;
        returns = new Date(d.getFullYear(),(d.getMonth()+1),0);
        return returns;
}

